/**
 * 
 */

package com.hanyee.percentProcess.widget;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

public class PercentProcessWidgetView extends View {

    private final static String TAG = "PercentProcessWidgetView";

    private Paint mPaint;
    private float left;
    private float top;
    private float bottom;
    private float width;

    private float[] percents;
    private ArrayList<String> dataList;
    boolean drawOver;

    public PercentProcessWidgetView(Context context) {
        super(context);
    }

    public PercentProcessWidgetView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PercentProcessWidgetView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        //Log.d(TAG, "PercentProcessWidgetView()");
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
    }

    private void updateDataParam(int what, float percent) {
        //Log.d(TAG, "updateDataParam() what=" + what);
        percents[what] = percent;
        invalidate();
    }

    private void quickSortArray(Integer[] colors, int left, int right) {
        if (left < right) {
            int s = colors[left];
            int i = left;
            int j = right + 1;
            while (true) {
                while (i + 1 < colors.length && colors[++i] < s)
                    ;
                while (j - 1 > -1 && colors[--j] > s)
                    ;
                if (i >= j)
                    break;
                colors[i] = colors[i] ^ colors[j];
                colors[j] = colors[i] ^ colors[j];
                colors[i] = colors[i] ^ colors[j];
            }
            colors[left] = colors[j];
            colors[j] = s;
            quickSortArray(colors, left, j - 1);
            quickSortArray(colors, j + 1, right);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //Log.d(TAG, "onDraw()");

        left = 0;
        top = 0;
        width = getWidth();
        bottom = getHeight();
        if(dataList==null)return ;
        int size = dataList.size();
        

        // Draw every percent part
        for (int i = size - 1; i >= 0; i--) {
            String item = dataList.get(i);
            int color = Integer.parseInt(item.substring(0, item.indexOf("|")));
            mPaint.setColor(color);
            canvas.drawRect(left, top, width * percents[i], bottom, mPaint);
        }
    }

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int what = msg.what;
            float data = (Float) msg.obj;
            updateDataParam(what, data);
        }

    };

    private class Calculate implements Runnable {

        private float destPercent;
        private int whatToDo;

        public Calculate(float destPercent, int whatToDo) {
            this.destPercent = destPercent;
            this.whatToDo = whatToDo;
        }

        @Override
        public void run() {
        	drawOver =false;
            float increase = 0f;
            while (increase <= destPercent) {
                increase += 0.01f * destPercent;
                Message msg = Message.obtain(mHandler, whatToDo, increase);
                msg.sendToTarget();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    public void parseDataAndPerform(ArrayList<String> data) {
        //Log.d(TAG, "parseDataAndPerform()");
        if (null == data || data.size() == 0) {
            //Log.d(TAG, "data is null !");
            return;
        }
        dataList = data;
        int size = dataList.size();
        percents = new float[size];
        float percentSum = 0f;
        for (int i = size - 1; i >= 0; i--) {
            String item = dataList.get(i);
            float percent = Float.parseFloat(item.substring(item.indexOf("|") + 1));
            new Thread(new Calculate(1 - percentSum, i)).start();
            percentSum += percent;
        }
    }
}
